import time

from downloader import DownloaderFactory
from rapidshare import RapidshareParser,RapidshareLogin
from eventmanager import EventManager, Topic, Singleton

from constants import Status

t = None

# df = DownloaderFactory()

# f = file("debian-card.iso","wb")

# url = "http://cdimage.debian.org/debian-cd/5.0.3/i386/iso-cd/debian-503-i386-businesscard.iso"

# df.createDownloadThread(url,f)

# t = df.getDownloadThreads()[0]

# lastTime = time.time()
# currentTime = time.time()

# while t.getStatus() != Status.Finished:
#     currentTime = time.time()
#     condition = currentTime-lastTime 
#     if condition > 5.0:
#         lastTime=currentTime
#         print t.info()


def startDownload(url,filename):
    lastTime = time.time()
    df = DownloaderFactory()
    print 'startDownload: %s %s' % (url, filename)
    global t 
    f = file(filename,"wb")
    t = df.createDownloadThread(url,f)



def error(errorstr):
    print errorstr

e = EventManager()
topic1 = e.addTopic('startDownloadTopic')
topic1.subscribe(startDownload)
topic2 = e.addTopic('errorTopic')
topic2.subscribe(error)
r = RapidshareLogin('USER','PASS') 
url = 'http://rs319.rapidshare.com/files/120356831/PK.3x12.100.wWw.Darkville.Com.Mx.By.Jhonyyy90.part1.rar'
parser = RapidshareParser(url, premium=True)
done = False
while not done:
    a = int(raw_input())
    if a == 1:
        print parser
    elif a == 2:
        print t.info()
    elif a == 0:
        done = True
        



